﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using MP3Ganizer.Common.Interfaces.FileTree;
using MP3Ganizer.Modules.FileTree.Views;
using MP3Ganizer.Modules.FileTree.ViewModels;
using MP3Ganizer.Common.BaseObject;
using MP3Ganizer.Common;
using Microsoft.Practices.Composite.Regions;
using Microsoft.Practices.Unity;
using Microsoft.Practices.Composite.Events;
using MP3Ganizer.Events.User;
using Microsoft.Practices.Composite.Presentation.Events;

namespace MP3Ganizer.Modules.FileTree.Controllers
{
    public class FileTreeController : ModuleControllerBase, IFileTreeController
    {
        public FileTreeController(IUnityContainer container, IRegionManager regionManager, IEventAggregator eventAggregator)
            : base(container, regionManager, eventAggregator)
        {
        }
        protected override void SubscribeCompositeEvent()
        {
            _container.Resolve<Microsoft.Practices.Composite.Events.IEventAggregator>().GetEvent<MP3Ganizer.Events.User.UserActionEvent>().Subscribe(OnUserAction, Microsoft.Practices.Composite.Presentation.Events.ThreadOption.UIThread, true);
        }

        public override void Run()
        {
            _regionManager.RegisterViewWithRegion(RegionNames.Center1, () => GetView());
            //_regionManager.RegisterViewWithRegion(RegionNames.Right, () => RegisterView());
        }

        private void OnUserAction(MP3Ganizer.Events.User.UserActionEventArgs e)
        {
            if (e.Action == MP3Ganizer.Common.Enums.UserAction.LoadFileTree)
            {

                if (!(e.Data is KeyValuePair<string, string>))
                    return;
                var pathes = (KeyValuePair<string, string>)e.Data;
                LoadTreeView(RegionNames.Center1, pathes.Key);
                //LoadTreeView(RegionNames.Right, pathes.Value);
            }
            if (e.Action == MP3Ganizer.Common.Enums.UserAction.RefreshFileTree)
            {
                IRegion region = _regionManager.Regions[RegionNames.Center1];
                var view = region.Views.FirstOrDefault(item => item is IFileTreeView);
                if (view == null)
                    return;
                FileTreeView typedView = GetView() as FileTreeView;
                IFileTreeViewModel viewModel = (IFileTreeViewModel)typedView.DataContext;
                viewModel.Reload();
            }
        }
            
        private void LoadTreeView(string regionName, string directory)
        {
            IRegion region = _regionManager.Regions[regionName];
            var view = region.Views.FirstOrDefault(item => item is IFileTreeView);
            if (view == null)
                Run();
            FileTreeView typedView = GetView() as FileTreeView;
            IFileTreeViewModel viewModel = (IFileTreeViewModel)typedView.DataContext;
            viewModel.SetDirectory(directory);
            viewModel.Initialize();
        }

        private IFileTreeView GetView()
        {
            FileTreeView view = _container.Resolve<FileTreeView>();
            return view;
        } 
    }
}
